package com.wanlian.router.ui.main.yun;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.google.gson.Gson;
import com.smart.android.ui.BaseFragment;
import com.smart.android.utils.FileUtils;
import com.wanlian.router.R;
import com.wanlian.router.bean.UpTask;
import com.wanlian.router.bean.UpTaskDatas;
import com.wanlian.router.bean.YunFile;
import com.wanlian.router.net.UploadServer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Hyu on 2018/9/23.
 * Email: fvaryu@163.com
 */
public class UploadFragment extends BaseFragment {


    @BindView(R.id.view_listview_0)
    ListView view_listview_0;

    List<UpTask> mYunFiles = new ArrayList<>();

    @BindView(R.id.view_listview_1)
    ListView view_listview_1;
    List<UpTask> mYunDoneFiles = new ArrayList<>();


    ProgressAdapter mProgressAdapter;
    ProgressDoneAdapter mProgressDoneAdapter;
    public static UploadFragment newInstance() {

        Bundle args = new Bundle();

        UploadFragment fragment = new UploadFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int layout() {
        return R.layout.fragment_upload;
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);

        view_listview_0.setAdapter(mProgressAdapter = new ProgressAdapter(getActivity(), mYunFiles));
        view_listview_1.setAdapter(mProgressDoneAdapter = new ProgressDoneAdapter(getActivity(), mYunDoneFiles));

    }

    @Override
    protected void initData(View view) {
        super.initData(view);
        try {

            UpTaskDatas upTaskDatas = UploadServer.readFile();
            if (upTaskDatas == null) {
                return;
            }
            List<UpTask> tasks = upTaskDatas.getUpTasks();
            for (UpTask task : tasks) {
                if (task.getLength() == task.getUpLength()) {
                    mYunDoneFiles.add(task);
                }else {
                    mYunFiles.add(task);
                }
            }
            mProgressAdapter.notifyDataSetChanged();
            mProgressDoneAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }



    }
}
